/*  This file is part of YATracker.
    YATracker is free software: you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
    YATracker is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License along with
YATracker. If not, see <http://www.gnu.org/licenses/>. */

#ifndef QMAPWIDGET_H
#define QMAPWIDGET_H

#include <QWidget>
#include <QDateTime>
#include <QTimer>
#include <QVector>

class YatMapFile;

struct YatMapCacheItem
{
    quint32 tilex, tiley;
    QDateTime lastaccesstime;
    QImage* pimage;
};

class QMapWidget : public QWidget
{
    Q_OBJECT
public:
    QMapWidget();
    ~QMapWidget();

public:
    void setMapFile(YatMapFile* mapfile);
    void setSpotPosition(double lat, double lon);
    void setTargetPosition(double lat, double lon);
    void resetTargetPosition();
    void centerTo(qint64 x, qint64 y);
    void centerToSpot();

protected:
    virtual void mousePressEvent(QMouseEvent *);
    virtual void mouseReleaseEvent(QMouseEvent *);
    virtual void mouseMoveEvent(QMouseEvent *);
    virtual void paintEvent(QPaintEvent *);

protected slots:
    void targetTimerTick();

private:
    QImage* getTileImage(quint32 x, quint32 y);
    void convertLatLonToXY(double lat, double lon, qint64* px, qint64* py);

private:
    YatMapFile* m_MapFile;
    QVector<YatMapCacheItem> m_cache;
    qint64 m_ViewportX, m_ViewportY;
    int m_MouseX, m_MouseY;
    qint64 m_spotx, m_spoty;
    qint64 m_targetx, m_targety;
    QTimer m_targettimer;
};

#endif // QMAPWIDGET_H
